Imports System
Imports System.Xml

Public Class AccountsMsg
	Inherits BaseMsg

	'<CPCAPI v="1.1">
	'      <AccountReq ReqID="testID" ProductID="LN"/>
	'</CPCAPI>
	'<CPCAPI v="1.1">
	'      <AccountRsp ReqID="testID" ProductID="LN">
	'            <PtyGrp>
	'                  <Pty ID="PIONEERACCT" R="24"/>
	'                  <Pty ID="ewt" R="1">
	'                        <Sub ID="Pioneer Futures, Inc." Typ="5"/>
	'                  </Pty>
	'                  <Pty ID="444" R="4">
	'                        <Sub ID="Test Clearing Firm" Typ="5"/>
	'                  </Pty>
	'            </PtyGrp>
	'            <PtyGrp>
	'                  <Pty ID="BARCLAYSACCT" R="24"/>
	'                  <Pty ID="barclayscapital" R="1">
	'                        <Sub ID="Barclays Capital Inc." Typ="5"/>
	'                  </Pty>
	'                  <Pty ID="444" R="4">
	'                        <Sub ID="Test Clearing Firm" Typ="5"/>
	'                  </Pty>
	'            </PtyGrp>

	Public Sub New()
		RequestType = "AccountReq"
		ResponseType = "AccountRsp"
	End Sub

	Protected Overrides Function RequestMsg(ByRef XmlW As XmlTextWriter) As Boolean

		If m_prodid = "" Then
			Return False
		End If

		XmlW.WriteAttributeString("ProductID", m_prodid)

		Return True
	End Function

	Protected Overrides Sub ResponseParse(ByRef node As XmlNode)

		Dim ptygrp As XmlNode
		Dim attr As XmlAttribute

		Dim dbi As dbInterface = Globals.dbi

		If node.Attributes("ProductID").InnerText = m_prodid Then

			dbi.nonquery("delete from nymx_prod_accounts where prod_id=" & dbi.quote(m_prodid))

			For Each ptygrp In node.ChildNodes
				InsertParty(ptygrp)
			Next
		End If

	End Sub

	Private Sub InsertParty(ByRef ptygrp As XmlNode)
		'            <PtyGrp>
		'                  <Pty ID="BARCLAYSACCT" R="24"/>
		'                  <Pty ID="barclayscapital" R="1">
		'                        <Sub ID="Barclays Capital Inc." Typ="5"/>
		'                  </Pty>
		'                  <Pty ID="444" R="4">
		'                        <Sub ID="Test Clearing Firm" Typ="5"/>
		'                  </Pty>
		'            </PtyGrp>

		Dim dbi As dbInterface = Globals.dbi
		Dim party As XmlNode
		Dim ptysub As XmlNode

		Dim acctid As String = ""
		Dim execid As String = ""
		Dim execnm As String = ""
		Dim clearid As String = ""
		Dim clearnm As String = ""
		Dim brokerid As String = ""
		Dim brokernm As String = ""

		Dim r As String
		Dim nm As String

		If ptygrp.Name = "PtyGrp" Then
			For Each party In ptygrp.ChildNodes
				r = party.Attributes("R").InnerText
				nm = ""

				If party.ChildNodes.Count > 0 Then
					For Each ptysub In party.ChildNodes
						If ptysub.Name = "Sub" Then
							nm = ptysub.Attributes("ID").InnerText
						End If
					Next
				End If

				Select Case r
					Case "24"
						acctid = party.Attributes("ID").InnerText

					Case "1"
						execid = party.Attributes("ID").InnerText
						execnm = nm

					Case "4"
						clearid = party.Attributes("ID").InnerText
						clearnm = nm

					Case "7"
						brokerid = party.Attributes("ID").InnerText
						brokernm = nm

				End Select

			Next

			dbi.SetupStoredProc("sp_nymx_ins_product_account")
			dbi.AddStoredProcParam("@prod_id", SqlDbType.VarChar, m_prodid)
			dbi.AddStoredProcParam("@acct_id", SqlDbType.VarChar, acctid)
			dbi.AddStoredProcParam("@exec_firm_id", SqlDbType.VarChar, execid)
			dbi.AddStoredProcParam("@exec_firm_name", SqlDbType.VarChar, execnm)
			dbi.AddStoredProcParam("@clear_firm_id", SqlDbType.VarChar, clearid)
			dbi.AddStoredProcParam("@clear_firm_name", SqlDbType.VarChar, clearnm)
			dbi.AddStoredProcParam("@broker_firm_id", SqlDbType.VarChar, brokerid)
			dbi.AddStoredProcParam("@broker_firm_name", SqlDbType.VarChar, brokernm)
			dbi.RunStoredProc()

		End If

	End Sub

End Class

